package net.atos.hibernate_spring;

import static org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType.HSQL;

import javax.sql.DataSource;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.orm.hibernate3.LocalSessionFactoryBean;
import org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean;

@Configuration
public class HibernateEmbeddedHSQLConfiguration {

    @Bean(destroyMethod = "shutdown")
    public DataSource dataSource() {
	final EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
	return builder.setType(HSQL).addScript("db-schema.sql").addScript("db-data.sql").build();
    }

    @Bean
    public LocalSessionFactoryBean sessionFactory() {
	final AnnotationSessionFactoryBean factory = new AnnotationSessionFactoryBean();
	factory.setDataSource(dataSource());
	factory.setAnnotatedClasses(new Class[] { Book.class });
	return factory;
    }
}
